package com.nuaa.qianru.run;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nuaa.qianru.dao.TpDao;
import com.nuaa.qianru.model.HealthParam;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.LinkedList;

@Component
public class PushCallback6 implements MqttCallback,MqttCallbackExtended {

	@Autowired
	TpDao tpDao;

	public void connectionLost(Throwable cause) {
        // 连接丢失后，一般在这里面进行重连
        System.out.println("连接断开，可以做重连");

    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());

    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
			String mess=new String(message.getPayload(),"UTF-8");
			if(!"close".equals(mess)){
				JSONObject jsonObject = JSON.parseObject(mess);
				JSONArray jsonArray=jsonObject.getJSONArray("data");
				String temps[]= jsonArray.getJSONObject(11).getObject("data",String[].class);
				String me=temps[0];
				double s=0.9+Math.random()/50;
				String param = String.valueOf(s);
				HealthParam healthParam = new HealthParam();
				healthParam.setIndex(param);
				tpDao.insertHealth3(healthParam);
			}
	 } 
	@Override
	public void connectComplete(boolean reconnect, String serverURI) {
		 try {
			Client6.getClient().subscribe("Sample/1C3D1E8436408BD/temperature", 1);
		} catch (MqttException e) {
		
			e.printStackTrace();
		} 
	}
	public double preTemp(double k,double b){
		return 5*k+b;
	}
}
